package com.nuc.bigdata.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nuc.bigdata.app.service.HomePageService;
import com.nuc.bigdata.domain.entity.ViewExhibitionComponent;
import com.nuc.bigdata.domain.entity.ViewPagerComponent;
import com.nuc.bigdata.infra.mapper.ViewExhibitionComponentMapper;
import com.nuc.bigdata.infra.mapper.ViewPagerComponentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.List;

/**
 * @author JackLuo
 * @date 2019/10/14
 */
@Service
public class HomePageServiceImpl implements HomePageService {


    private static final String SIMPLEEXHIBITION="SimpleExhibition";
    private static final String EXHIBITION="Exhibition";
    private static final String PAGER="Pager";

    private static final String TITLE="Title";
    private static final String INTRODUCE="Introduce";
    private static final String BACKGROUND="Background";
    private static final String PICTURE="Picture";
    private static final String BUTTON="Button";
    private static final String AUTHOR="Author";

    private static final String SIMPLE="simple";
    private static final String DETAILS="details";

    @Autowired
    private ViewExhibitionComponentMapper viewExhibitionComponentMapper;

    @Autowired
    private ViewPagerComponentMapper viewPagerComponentMapper;

    @Override
    public void init(ModelMap map) {
        initViewPagerComponent(map);
        initExhibitionComponent(map,SIMPLE);
        initExhibitionComponent(map,DETAILS);

    }

    private void initExhibitionComponent(ModelMap map,String type) {
        QueryWrapper<ViewExhibitionComponent> vecQueryWrapper = new QueryWrapper<>();
        vecQueryWrapper.eq("type",type ).orderByAsc("rank");
        List<ViewExhibitionComponent> vecList = viewExhibitionComponentMapper.selectList(vecQueryWrapper);
        if (SIMPLE.equals(type)) {
            initCirculation(vecList,map ,SIMPLEEXHIBITION );
        }else if (DETAILS.equals(type)) {
            initCirculation(vecList,map ,EXHIBITION );
        }

    }

    private void initCirculation(List<ViewExhibitionComponent> vecList,ModelMap map,String type){
        for (int i = 1; i <= vecList.size(); i++) {
            ViewExhibitionComponent vec = vecList.get(i - 1);
            map.addAttribute(type+i+TITLE, vec.getTitle());
            map.addAttribute(type+i+INTRODUCE, vec.getIntroduce());
            map.addAttribute(type+i+AUTHOR, vec.getAuthorName());
            map.addAttribute(type+i+PICTURE, vec.getPicture());

        }
    }

    private void initViewPagerComponent(ModelMap map) {

        QueryWrapper<ViewPagerComponent> viewPagerComponentQueryWrapper = new QueryWrapper<>();
        viewPagerComponentQueryWrapper.eq("type", "button").orderByAsc("rank");
        List<ViewPagerComponent> vpcList = viewPagerComponentMapper.selectList(viewPagerComponentQueryWrapper);


        for (int i = 1; i <= vpcList.size(); i++) {
            ViewPagerComponent vpc = vpcList.get(i - 1);
            map.addAttribute(PAGER+i+TITLE, vpc.getTitle());
            map.addAttribute(PAGER+i+INTRODUCE, vpc.getIntroduce());
            map.addAttribute(PAGER+i+BUTTON, vpc.getButtonContext());
            map.addAttribute(PAGER+i+BACKGROUND, vpc.getPicture());

        }

    }
}
